// 全局 对象 --  用于挂载 一些 经常 用到的 变量、函数  ...

import {
  tokenName,
  getToken,
  setToken,
  formatSearchParam,
  version
} from '@/plugin/baseInfo'

import {
  loadJs,
  loadCss,
  getStreamViewUrl
} from '@/plugin/utils'

import { nanoid } from 'nanoid'

import { toNumber, cloneDeep, debounce } from 'lodash'

import axios from '@/plugin/axios'

import previewFile from '@/plugin/previewFile'

import createInstance from '@/plugin/createInstance'


const $g = {
  createInstance,
  previewFile,
  test(){

  },
  getDicts: function(list = []){
    const item = {}
    const paramList = list.filter(r => {
      if (r in $g.dicts)  {             //  将原本 内存中 缓存 有的 接口 数据 返回
        item[r] = $g.dicts[r]
        return false
      }
      return true
    })

    if (paramList.length > 0) {
      return new Promise((resolve, reject) => {
        $g.post('/basics/basics/dict/list', {
          dicTypes: paramList.join(','),
          dictBusiType: 'BUSI_DICT'
        }).then(res => {
          resolve(Object.assign(item, res.body || {}))
        }).catch(err => {
          resolve(Object.assign(item, res.body || {}))
        })
      })
    }

    return $g.cloneDeep(item)
  },
  dicts: {},        //  存放全局字典 -- 注意： 这里 存放的是 通过 ajax 调用字典接口 返回的字典数据，刷新后会清空。
  post:function(...args){
    return axios.post(...args)
  },
  get: function(...args){
    return axios.get(...args)
  },
  loadJs, //  动态 加载 js
  loadCss, //  动态 加载  css
  getStreamViewUrl,
  tokenName,
  getToken,
  setToken,
  nanoid,
  formatSearchParam,
  version,
  toNumber,
  cloneDeep,
  debounce
}

export default $g